//
//  snapFooter.m
//  jinyishe
//
//  Created by 金艺社 on 2018/5/7.
//  Copyright © 2018年 wy. All rights reserved.
//

#import "snapFooter.h"
#import "GKCustomAlertView.h"
#import "NetworkManager.h"
#import "ResetLoginPasswordVC.h"
#import "setTradingPassword.h"
#import <AlipaySDK/AlipaySDK.h>
@interface snapFooter ()
@property (nonatomic, weak) GKCustomAlertView *alertV;
@property (nonatomic, assign) BOOL isOver;
@end

@implementation snapFooter


- (IBAction)ConfirmBuy:(id)sender {
    if (self.needChongZhi > 0.00) {//使用支付宝
        [self aliPayMoney];
    }else {//余额支付
        [self isSetTradePassword];//先判断设置交易密码
    }
}
#pragma mark --- 支付宝支付
- (void)aliPayMoney {
    NSDictionary *dic = @{@"token":USERTOKEN,@"hongbaoId":self.hongBaoId};
    NSString *url = [NSString stringWithFormat:@"%@%@", [[apiManger shareApiManger] aliPayPayServiceCharge], self.orderId];
    __weak typeof(self) weakSelf = self;
    [networkManger PostHttpDataWithUrlStr:url Dic:dic SuccessBlock:^(id responseObject) {
        if ([responseObject[@"rcd"] isEqualToString:R0001]) {
            [weakSelf playClickWithOrder:responseObject [@"data"]];
        }else{
            [MBProgressHUD gk_toastShowMessage:responseObject[@"rmg"]];
        }
    } FailureBlock:^(id error) {
        [MBProgressHUD gk_toastShowMessage:@"请求失败"];
    }];
}
- (void)playClickWithOrder:(NSString *)order {
    [[AlipaySDK defaultService] payOrder:order fromScheme:AppScheme callback:^(NSDictionary *resultDic) {
        NSLog(@"reslut = %@",resultDic);
    }];
}

#pragma mark ---  余额支付
//判断是否设置交易密码
- (void)isSetTradePassword {
    NSDictionary *dic = @{ @"token":USERTOKEN };
    __weak typeof(self) weakSelf = self;
    NSString *url = [NSString stringWithFormat:@"%@", [[apiManger shareApiManger] isSetPayPassword]];
    [networkManger PostHttpDataWithUrlStr:url Dic:dic SuccessBlock:^(id responseObject) {
        if ([responseObject[@"rcd"] isEqualToString:R0001]) {
            NSString *flg = [NSString stringWithFormat:@"%@", responseObject[@"flg"]];
            if ([flg isEqualToString:@"1"]) {
                [weakSelf goInvest];
            }else {
                [weakSelf popSetTradView];
            }
        }else {
            [MBProgressHUD gk_toastShowMessage:responseObject[@"rmg"]];
        }
    } FailureBlock:^(id error) {
        [MBProgressHUD gk_toastShowMessage:@"请求失败"];
    }];
}

//去设置交易密码
- (void)popSetTradView {
    setTradingPassword *VC = [setTradingPassword new];
    [[helpPublic getCurrentViewController:self].navigationController pushViewController:VC animated:YES];
}

//去支付
- (void)goInvest
{
    __weak typeof(self)weakSelf = self;
    self.alertV = [GKCustomAlertView showTradePasswordAlertWithMessage:@"交易确认" completionBlock:^(GKCustomAlertView *alertView, NSInteger buttonIndex) {
        if (buttonIndex == 1) {
            if(weakSelf.isOver) {
                [alertView dismissAlertView];
                [[helpPublic getCurrentViewController:weakSelf].navigationController popToRootViewControllerAnimated:YES];
            }else {
                [alertView dismissAlertView];
                NSString *msg = [alertView.passwordView.passwordTF.text transactionPasswordReguler];
                if (msg.length == 0) {
                    NSString *url;
                    NSDictionary *parames;
                    url = [NSString stringWithFormat:@"%@%@", [[apiManger shareApiManger] zhiJieZhiFuPay], self.orderId];
                    parames = @{@"token":USERTOKEN,
                                @"hongbaoId" : self.hongBaoId,
                                @"payPassWord" : alertView.passwordView.passwordTF.text};
                    [[NetworkManager defaultManager] check_getWithURLString:url parameters:parames success:^(id responseObject) {
                        [self handleConfirmResponseData:responseObject];
                    } failure:^(NSError *error) {
                        [MBProgressHUD gk_toastShowMessage:@"网络出错了"];
                    }];
                } else {
                    alertView.passwordView.errorLable.text = msg;
                }
            }
        } else if (buttonIndex == 2) {//忘记交易密码
            [alertView dismissAlertView];
            ResetLoginPasswordVC *VC = [ResetLoginPasswordVC new];
            VC.title = @"重置交易密码";
            [[helpPublic getCurrentViewController:self].navigationController pushViewController:VC animated:YES];
        }
    }];
}
- (void)handleConfirmResponseData:(NSDictionary *)responseObject {
    if ([responseObject[@"rcd"] isEqualToString:R0001]) {//支付成功
        [self pushResultVCWithType:@"11"];
        [self postNotic];
    } else {
       [MBProgressHUD gk_toastShowMessage:responseObject[@"rmg"]];
    }
}
- (void)postNotic {
    [[NSNotificationCenter defaultCenter] postNotificationName:@"paySuccessful" object:nil];
}
- (void)pushResultVCWithType:(NSString *)type {
    resultVC *VC = [resultVC new];
    VC.resultType = type;
    [[helpPublic getCurrentViewController:self].navigationController pushViewController:VC animated:YES];
}
@end
